package com.xinzhidi.web.equipmentAlarm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.equipmentAlarm.domain.EquipmentAlarm;
import com.xinzhidi.web.equipmentAlarm.domain.dto.EquipmentAlarmAddDto;
import com.xinzhidi.web.equipmentAlarm.domain.dto.EquipmentAlarmListDto;
import com.xinzhidi.web.equipmentAlarm.domain.dto.EquipmentAlarmUpdDto;
import com.xinzhidi.web.equipmentAlarm.domain.vo.EquipmentAlarmInfoVo;
import com.xinzhidi.web.equipmentAlarm.domain.vo.EquipmentAlarmListVo;
import com.xinzhidi.web.equipmentAlarm.service.EquipmentAlarmService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 设备告警Controller
 *
 * @author ruoyi
 * @date 2023-05-31
 */
@RestController
@Api(tags = "设备告警")
@RequestMapping("/equipmentAlarm")
public class EquipmentAlarmController extends BaseController {
    @Autowired
    private EquipmentAlarmService equipmentAlarmService;

    @ApiOperation("查询设备告警列表")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:list')")
    @PostMapping("/listEquipmentAlarm")
    public TableDataInfo<EquipmentAlarmListVo> listEquipmentAlarm(@Validated @RequestBody EquipmentAlarmListDto dto) {
        startPagePost(dto);
        return equipmentAlarmService.selectEquipmentAlarmList(dto);
    }

    @ApiOperation("导出设备告警列表")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:export')")
    @Log(title = "设备告警", businessType = BusinessType.EXPORT)
    @PostMapping("/exportEquipmentAlarm")
    public void exportEquipmentAlarm(HttpServletResponse response, EquipmentAlarmListDto dto) {
        TableDataInfo<EquipmentAlarmListVo> list = equipmentAlarmService.selectEquipmentAlarmList(dto);
        ExcelUtil<EquipmentAlarmListVo> util = new ExcelUtil<EquipmentAlarmListVo>(EquipmentAlarmListVo.class);
        util.exportExcel(response, list.getRows(), "设备告警数据");
    }

    @ApiOperation("获取设备告警详细信息")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:query')")
    @GetMapping(value = "/getEquipmentAlarmById")
    public R<EquipmentAlarmInfoVo> getEquipmentAlarmById(@RequestParam("id") Long id) {
        return R.ok(equipmentAlarmService.selectEquipmentAlarmById(id));
    }

    @ApiOperation("新增设备告警")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:add')")
    @Log(title = "设备告警", businessType = BusinessType.INSERT)
    @PostMapping("addEquipmentAlarm")
    public AjaxResult addEquipmentAlarm(@Validated @RequestBody EquipmentAlarmAddDto dto) {
        return toAjax(equipmentAlarmService.insertEquipmentAlarm(dto));
    }

    @ApiOperation("修改设备告警")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:edit')")
    @Log(title = "设备告警", businessType = BusinessType.UPDATE)
    @PostMapping("editEquipmentAlarm")
    public AjaxResult editEquipmentAlarm(@Validated @RequestBody EquipmentAlarmUpdDto dto) {
        return toAjax(equipmentAlarmService.updateEquipmentAlarm(dto));
    }

    @ApiOperation("删除设备告警")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:remove')")
    @Log(title = "设备告警", businessType = BusinessType.DELETE)
    @DeleteMapping("/delEquipmentAlarm")
    public AjaxResult delEquipmentAlarm(@RequestParam Long[] ids) {
        return toAjax(equipmentAlarmService.deleteEquipmentAlarmByIds(ids));
    }

    @ApiOperation("设备告警推送列表")
    @PreAuthorize("@ss.hasPermi('equipmentAlarm:equipmentAlarm:list')")
    @PostMapping("/listEquipmentAlarmPush")
    public AjaxResult listEquipmentAlarmPush() {
        QueryWrapper<EquipmentAlarm> wrapper = new QueryWrapper<>();
        wrapper.eq("hide_or_push", 1);
        return AjaxResult.success(equipmentAlarmService.list(wrapper));
    }

    @ApiOperation("消息推送定时任务")
    @GetMapping("/messagePush")
    public AjaxResult messagePush() {
        equipmentAlarmService.messagePush();
        return AjaxResult.success();
    }

}
